/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onlylemi.mapview.menu;

import com.onlylemi.mapview.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import java.util.List;

/**
 * 菜单dialog
 *
 * @since 2021-06-03
 */
public class MenuDialog extends PopupDialog {
    private static Component view;
    private static Context context;
    private static List<String> dataList;

    private OnItemClickListener listener;

    /**
     * 构造函数
     *
     * @param context
     * @param contentComponent
     */
    public MenuDialog(Context context, Component contentComponent) {
        super(context, contentComponent);
    }

    /**
     * 获取dialog
     *
     * @param context
     * @return MenuDialog
     */
    public static MenuDialog with(Context context) {
        MenuDialog.context = context;
        view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_menu, null, false);
        return new MenuDialog(context, view);
    }

    /**
     * 加载数据
     *
     * @param dataList
     * @return MenuDialog
     */
    public MenuDialog load(List<String> dataList) {
        setDataList(dataList);
        return this;
    }

    /**
     * 设置监听
     *
     * @param listener
     * @return MenuDialog
     */
    public MenuDialog setListener(OnItemClickListener listener) {
        this.listener = listener;
        return this;
    }

    /**
     * 显示dialog
     */
    public void showDialog() {
        ListContainer lvMenu = (ListContainer) view.findComponentById(ResourceTable.Id_lv_menu);
        lvMenu.setLongClickable(false);
        lvMenu.setEnabled(false);
        MenuProvider menuProvider = new MenuProvider(context, dataList);
        lvMenu.setItemProvider(menuProvider);
        lvMenu.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                if (listener != null) {
                    listener.onItemClick(menuProvider, i);
                    menuProvider.notifyDataChanged();
                    destroy();
                }
            }
        });

        setCustomComponent(view);
        setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        setAutoClosable(true);
        setCornerRadius(8);
        setBackColor(Color.TRANSPARENT);
        setHasArrow(true);
        showOnCertainPosition(LayoutAlignment.RIGHT | LayoutAlignment.TOP, 11, 92);

        view.setPivotX(view.getWidth());
        view.setPivotY(0f);
        view.createAnimatorProperty()
                .scaleXFrom(0.75f).scaleYFrom(0.75f)
                .scaleX(1.0f).scaleY(1.0f)
                .setDuration(350)
                .start();
    }

    /**
     * item监听
     *
     * @since 2021-06-03
     */
    public interface OnItemClickListener {
        /**
         * item点击监听
         *
         * @param adapter
         * @param position
         */
        void onItemClick(MenuProvider adapter, int position);
    }

    private static void setDataList(List<String> dataList) {
        MenuDialog.dataList = dataList;
    }
}
